##
rm(list=ls())
gc()


library(data.table)
library(ggplot2)
library(dplyr)
library(stringr)

files = paste0('results/',c('future-results-2008-2012.Rdata',
                                        'future-results-2012-2016.Rdata',
                                        
                                        'future-results-2008-2012-alternative-subsets-1.Rdata',
                                        'future-results-2012-2016-alternative-subsets-1.Rdata',
                                        'future-results-2008-2012-alternative-subsets-2.Rdata',
                                        'future-results-2012-2016-alternative-subsets-2.Rdata'))



results = rbindlist(lapply(files, FUN=function(file){

  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
   # print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    out[,N:=m$N]
    out[,`R-Squared`:=m$r.squared]
    out[,`Adjusted R-Squared`:=m$adj.r.squared]
    out[,Sigma:=m$sigma]
    out[,FStat:=m$fstat]
    if('Cluster s.e.'%in%names(out)){
      out[,SE:=`Cluster s.e.`]
      out[,SE.type:='Cluster']
      out[,`Cluster s.e.`:=NULL]
    } else {
      out[,SE:=`Robust s.e`]
      out[,SE.type:='Robust']
      out[,`Robust s.e`:=NULL]
    }


return(out)

  }))
  
l[,Years:=case_when(grepl('2008-2012',file)~ '2008-2012',
                    grepl('2012-2016',file)~ '2012-2016')]  
l[,file:=file]
l[,Sample:=ifelse(grepl('-subsets-1',file),'Year 1 subsets',ifelse(grepl('-subsets-2',file),'Year 3 - Year 1 party switching' ,'Main'))]
l[,Year1:=substr(Years,1,4)]

return(l)
}))
#results[,Sample:=factor(Sample,levels=c('Main','Alternative'))]

results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh','DemSpExpDiff_nohh','RepSpExpDiff_nohh')]


results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']
results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

results[grepl('Dems',Model),Subset:='Democrats']
results[grepl('Reps',Model),Subset:='Republicans']
results[grepl('Oths',Model),Subset:='Non-partisans']

colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")


g1 = ggplot(results, aes(y = Estimate, x = Subset, color = Subset, shape = Sample))+
  geom_point(size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Coefficient on change in partisan exposure')+
  xlab('Original partisan registration')+
  guides(color = 'none')+
  theme(text = element_text(size = 15, family = 'serif'), strip.text.y = element_text(size=12,angle=0),legend.title=element_blank(),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Years)

ggsave(plot = g1,filename = 'figures/FigS11.png',dpi=300, width = 11, height = 8, units = 'in')

